package com.szkiti.myautils.utils;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class BitmapUtil {
	
	public static final int LEFT = 0x01;
	public static final int RIGHT = 0x02;
	public static final int TOP = 0x03;
	public static final int BOTTOM = 0x04;
	
	/**
	 * 将Drawable转换为Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}
 /**
  * 将Bitmap转换为 byte[]
  * @param bm
  * @return
  */
	public static byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
   /**
    * 将 byte[]转换为bitmap
    * @param b
    * @return
    */
	public static Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}
    /**
     * 将bitmap转化为Drawable
     * @param bitmap
     * @return
     */
	public static Drawable bitmapToDrawable(Bitmap bitmap) {
		BitmapDrawable drawable = new BitmapDrawable(bitmap);
		return (Drawable) drawable;
	}
	/**
	 * 根据图片的宽度比列来缩放图片
	 * @param resourse
	 * @param desWith
	 * @return
	 */
	public static Bitmap scaleBitMapByWidth(Bitmap source,int desWith) {
		int rWidth = source.getWidth();
		int rHeight = source.getHeight();
		float rat = (float)rWidth / (float)desWith;
		Matrix matrix = new Matrix();
		matrix.postScale(rat, rat);
	    Bitmap bm = Bitmap.createBitmap(source, 0, 0, rWidth, rHeight, matrix, true);
	    return bm;
	}
	/**
	 * 根据图片的高度比列来缩放图片
	 * @param resourse
	 * @param desWith
	 * @return
	 */
	public static Bitmap scaleBitMapByHeight(Bitmap source, int desHeight) {
		int rWidth = source.getWidth();
		int rHeight = source.getHeight();
		float rat = (float)rHeight / (float)desHeight;
		Matrix matrix = new Matrix();
		matrix.postScale(rat, rat);
	    Bitmap bm = Bitmap.createBitmap(source, 0, 0, rWidth, rHeight, matrix, true);
	    return bm;
	}
	/**
	 * 将图片缩放到规定宽度和高度大小的尺寸中
	 * @param source
	 * @param desWidth
	 * @param desHeight
	 * @return
	 */
	public static Bitmap scaleBitmap(Bitmap source, int desWidth, int desHeight) {
		int rWidth = source.getWidth();
		int rHeight = source.getHeight();
		float sx = (float)rWidth/(float)desWidth;
		float sy = (float)rHeight/(float)desHeight;
		Matrix matrix = new Matrix();
		matrix.postScale(sx, sy);
		Bitmap bm = Bitmap.createBitmap(source, 0, 0, rWidth, rHeight, matrix, true);
		return bm;
	}
	/**
	 * 图片的合成方法
	 * @param first 第一张图片
	 * @param second 第二张图片
	 * @param direction 第二张图片在第一张图片的上面位置  LEFT,RIGHT,TOP,BOTTOM
	 * @return
	 */
	public static Bitmap compoundBitmap(Bitmap first, Bitmap second, int direction) {
		if(first == null && second == null) {
			return null;
		} 
		if(first == null && second != null) {
			return second;
		}
		if(first != null && second == null) {
			return first;
		}
		int firstWidth = first.getWidth();
		int firstHeight = first.getHeight();
		
		int secondWidth = second.getWidth();
		int secondHeight = second.getHeight();
		Bitmap newBitmap = null;
		Canvas canvas = null;
		switch (direction) {
		case LEFT:
			newBitmap = Bitmap.createBitmap(firstWidth + secondWidth , firstHeight > secondHeight ? firstHeight : secondHeight, Config.ARGB_8888);
			canvas = new Canvas(newBitmap);
			canvas.drawBitmap(second, 0, 0, null);
			canvas.drawBitmap(first, secondWidth, 0, null);
			canvas.restore();
			break;
		case RIGHT:
			newBitmap = Bitmap.createBitmap(firstWidth + secondWidth , firstHeight > secondHeight ? firstHeight : secondHeight, Config.ARGB_8888);
			canvas = new Canvas(newBitmap);
			canvas.drawBitmap(first, 0, 0, null);
			canvas.drawBitmap(second, firstWidth, 0, null);
			canvas.restore();
			break;
		case TOP:
			newBitmap = Bitmap.createBitmap(firstWidth > secondWidth ? firstWidth : secondWidth, firstHeight + secondHeight, Config.ARGB_8888);
			canvas = new Canvas(newBitmap);
			canvas.drawBitmap(second, 0, 0, null);
			canvas.drawBitmap(first, 0, secondHeight, null);
			canvas.restore();
			break;
		case BOTTOM:
			newBitmap = Bitmap.createBitmap(firstWidth > secondWidth ? firstWidth : secondWidth, firstHeight + secondHeight, Config.ARGB_8888);
			canvas = new Canvas(newBitmap);
			canvas.drawBitmap(first, 0, 0, null);
			canvas.drawBitmap(second, 0, firstHeight, null);
			canvas.restore();
			break;
		default:
			break;
		}
		return newBitmap;
	}
	/**
	 * 将图片转换为圆角图片
	 * @param bitmap
	 * @param pixels  圆角的弧度
	 * @return
	 */
	public static Bitmap roundCornerBitmap(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		int color = 0xff424242;
		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		RectF rectf = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectf, pixels, pixels, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);   
		return output;
	}
}
